#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/aes.h>
#include "base64.h"
#define AES_BLOCK_SIZE  16

unsigned char   data_downmode[98]   = {0};

int aesdecode(const char  *arg)
{
    AES_KEY aes;
    unsigned char key[AES_BLOCK_SIZE];        // AES_BLOCK_SIZE = 16
    unsigned char iv[AES_BLOCK_SIZE];        // init vector


    int i = 0;

     // Generate AES 128-bit key
    for (i=0; i<16; ++i) {
        key[i] = 32 + i;
    }
    // Set decryption key
    for (i=0; i<AES_BLOCK_SIZE; ++i) {
        iv[i] = 0;
    }
    if (AES_set_decrypt_key(key, 128, &aes) < 0) {
        fprintf(stderr, "Unable to set decryption key in AES\n");
       return -1;
    }

	unsigned char debase64[4096] = {0};
    int delen =   base64_decode(arg,debase64);

	printf("delen = %d\n", delen);
	
    int baselen = strlen(arg);

    int t = 0;

    printf("debase64:\n");

    for (t=0; t<baselen; ++t) {
    printf("%x%x", (debase64[t] >> 4) & 0xf,debase64[t] & 0xf);    
    }
    printf("\n");

    unsigned char debasedeco[4096] = {0};
    AES_cbc_encrypt(debase64, debasedeco,delen, &aes, iv, 
            AES_DECRYPT);

    printf("debase64 decrypted string: \n");

	int n = 0;
	for (n=0; n<delen; n++)
	{
		printf("%02x ",debasedeco[n]);
	}
	printf("\n");

        // addMprListQueue(NULL,data_downmode,98,0,CONN_SERVER_XIAOMI);
    //set_mode_down(debasedeco);


	return 0;
}


int main(int argc, char const *argv[])
{
    

    aesdecode(argv[1]);


    return 0;
}